package com.dzsw.redis.service.impl;

import com.dzsw.redis.service.IBizService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@Service
public class BizServiceImpl implements IBizService {

    @Resource
    private RedisLockRegistry redisLockRegistry;

    @Override
    public String handle(String msg) {
        Lock lock = redisLockRegistry.obtain("MY_LOCK");
        try {
            lock.lock();
            log.info(msg);
            TimeUnit.SECONDS.sleep(15);
            return "ok";
        } catch (InterruptedException e) {
            e.printStackTrace();
            return "fail";
        } finally {
            lock.unlock();
        }
    }
}
